/**
 * 基于UEditor开发的H5图文页面编辑器
 * Create by YaoQiao on 2017-01-11
 */

var htmlObject = {};
(function(){
	init();
})();


function init(){
	createEditor();
	addEvent();
}

/**
 * 添加事件侦听
 */
function addEvent(){
	//添加预览功能
	addPreviewFun();
}

/**
 * 创建编辑器
 */
function createEditor(){
	//初始化编辑
	htmlObject.editor = ue;
}


/**
 * 添加预览功能
 */
function addPreviewFun(){
	var pre_btn = $("#btn-preview");
	var pre_layer = $(".preview-layer");
	var pre_bg = $(".preview-bg");	
	pre_btn.on("click",function(){
		updatePreviewHtml();
		pre_layer.show();
		resetPrePhoneCss();
	});
	pre_bg.on("click",function(){
		pre_layer.hide();
	});
	
	//预览图片居中样式	
	var css_str = {};
	var pre_phone = $(".preview-phone");
	var pos_left = 0;
	var pos_top = 0;	
	$(window).resize(resetPrePhoneCss);
	//重置预览手机页面的CSS
	function resetPrePhoneCss(){
		pos_left = $(window).width() / 2 - pre_phone.width() / 2;
		pos_top = $(window).height() / 2 - pre_phone.height() / 2;
		css_str = {
			left:pos_left + "px",
			top:pos_top + "px"
		}
		pre_phone.css(css_str);
	}
	//初始化时设置预览手机元素位置
	resetPrePhoneCss();
}	

/**
 * 更新预览页面显示内容
 */
function updatePreviewHtml(){
	var ifr_document = document.getElementById("preview-html").contentWindow.document;	
	if(ifr_document){
		//设置标题
		var title_str = $("#article-title").val();
		var ifr_title = $(ifr_document).find(".article-title .title");
		ifr_title.html(title_str);
		//设置作者
		var author_str = $("#article-author").val();
		var ifr_author = $(ifr_document).find(".article-top .article-time");
		ifr_author.html(author_str);
		
	}
	//设置正文
	if(ifr_document && htmlObject.editor){
		var content_str = htmlObject.editor.getContent();
		var ifr_content = $(ifr_document).find(".article-content");
		ifr_content.html(content_str);
	}
}
